<template>
  <div class="addfarmsStock">
    <div class="back" @click="back()"><i class="el-icon-arrow-left"></i> 返回</div>
    <el-form
      :model="addForm"
      :rules="rules"
      ref="ruleForm"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="农资" prop="name" class="name">
        <el-input
          v-model="addForm.name"
          v-if="inputShow"
          :disabled="true"
        >
        </el-input>

        <el-button :class="{addBtn:inputShow}" @click="addBtn()">{{
          this.addForm.name === null ? "添加农资" : "重新选择"
        }}</el-button>
      </el-form-item>
      <el-form-item label="规格">
        <el-input v-model="addForm.specifications" :disabled="true">
        </el-input>
      </el-form-item>
      <el-form-item label="入库数量" prop="num">
        <el-input v-model="addForm.num" type="number" :min="1" >
        </el-input>
      </el-form-item>
      <!-- 入库类型字典 -->
      <el-form-item label="入库类型" prop="receiptType">
        <el-radio-group v-model="addForm.receiptType">
          <el-radio v-for="item in RKLX" :key="item.id" :label="item.code">{{item.name}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入库时间" prop="purchaseTime">
        <el-date-picker
          v-model="addForm.purchaseTime"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库人" prop="purchaser">
        <el-select v-model="addForm.purchaser" placeholder="请选择入库人">
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据、凭证照片">
        <upload ref="upload" :fileList="addForm.picture"></upload>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="textarea"
          placeholder="请输入备注"
          v-model="addForm.remark"
          style="width: 3.5rem"
          :rows="3"
          maxlength="30"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 农资弹窗 -->
    <el-dialog
      title="选择农资"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
      custom-class="farmsStockDialog"
    >
      <farmStockDialog
        :farmStockList="farmStockList"
        @farmsStockData="farmsStockData"
        :getAgriculturalDict="getAgriculturalDict"
        :total="total"
      ></farmStockDialog>
    </el-dialog>
  </div>
</template>

<script>
import { getAgriculturalDictApi, getUserListApi, getSysSonDictListApi } from '@/api/dict'
import { AddFarmStockApi, delpictureApi } from '@/api/farmStock'
import upload from './components/upload.vue'
import farmStockDialog from './components/farmStockDialog'
export default {
  name: 'SevencolorUserIndex',
  components: {
    upload, // 上传组件
    farmStockDialog
  },

  data () {
    return {
      addForm: {
        baseId: 1,
        agriDicId: null, // 农资ID
        name: null, // 农资名称
        num: null, // 农资数量
        specifications: null,
        purchaser: null, // 入库人--字典
        purchaseTime: null, // 入库时间
        receiptType: null, // 入库类型--字典
        picture: [],
        remark: null,
        price: null, // 单价
        totalPrice: null // 总数
      },
      params: {
        current: 1,
        pageSize: 8
      }, // 农资字典查询参数
      total: null,
      baseId: 1,
      addMissionData: {},
      rules: {
        name: [{ required: true, message: '请选择农资', trigger: 'blur' }],
        num: [{ required: true, message: '请选择农资', trigger: 'blur' }],
        receiptType: [{ required: true, message: '请选入库类型', trigger: 'blur' }],
        purchaseTime: [{ required: true, message: '请选择入库时间', trigger: 'blur' }],
        purchaser: [
          { required: true, message: '请选择入库人', trigger: 'blur' }
        ]
      },
      options: [],
      userList: [], // 用户字典
      dialogVisible: false, // 农资弹窗
      farmStockList: [], // 农资字典,
      inputShow: false, // 添加农资按钮显示,
      RKLX: []// 入库类型
    }
  },
  computed: {
    pictureList () {
      return this.$store.getters.pictureList
    },
    landDetail () {
      return this.$store.getters.landDetail
    },
    missionDetail () {
      return this.$store.getters.missionDetail
    }
  },

  mounted () {
    this.addForm.baseId = this.$store.getters.userInfo.baseId
    this.getAgriculturalDict(this.params)
    this.addMissionData = this.$route.params
    this.editForm()
    this.getUserList()
    this.getSysSonDictList()
  },

  methods: {
    // 字典接口
    async getAgriculturalDict (params) {
      // 农资字典
      const res = await getAgriculturalDictApi(params)
      console.log(res, '农资')
      this.farmStockList = res.data.records
      this.total = res.data.total
    },
    //
    async getSysSonDictList () {
      const res = await getSysSonDictListApi('RKLX')
      this.RKLX = res.data.reverse()
    },
    // 用户list
    async getUserList () {
      const res = await getUserListApi(this.baseId)
      // console.log(res)
      this.userList = res.data
    },
    // 返回
    back () {
      this.$router.push({
        path: '/shuzhongtian/farmsStock'
      })
    },
    addBtn () {
      this.dialogVisible = !this.dialogVisible
    },
    // 子组件事件--添加农资
    farmsStockData (data) {
      console.log(data, '选中的农资')
      this.addForm.name = data.name
      this.addForm.agriDicId = data.id
      this.addForm.specifications = data.specifications
      this.addForm.type = data.typeCode
      this.inputShow = true
      this.dialogVisible = false
    },
    // 提交表单
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$message('submit!')
          this.addForm.picture = this.pictureList.join(',')
          this.addForm.num = +this.addForm.num
          console.log(this.addForm)
          const res = await AddFarmStockApi(this.addForm)
          console.log(res)
          this.addForm = {}
          // this.$router.push({
          //   path: '/shuzhongtian/farmsStock'
          // }) // 返回详情
        } else {
          this.$message.error('请正确填写')
          return false
        }
      })
    },
    // 操作人下拉事件
    onSelectChange (value) {
      const userInfo = this.userList.filter((item) => item.userName === value)
      // console.log(userInfo)
      this.addForm.participantsId = userInfo[0].userId
    },
    // 农资弹窗关闭
    handleClose (dome) {
      dome()
    },
    // 重置表单
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.$refs.upload.clearFiles()
      if (this.pictureList.length > 0) {
        for (let i = 0; i < this.pictureList.length; i++) {
          delpictureApi(this.pictureList[i])
        }
        this.$store.dispatch('mission/removePictureList')
      }
    },
    // 编辑
    editForm () {
      if (this.missionDetail.id !== null) {
        // console.log(this.missionDetail, '农事')
        this.addForm.code = this.missionDetail.code
        this.addForm.participants = this.missionDetail.participants
        // this.addForm.area = this.missionDetail.area
        this.addForm.time = this.missionDetail.time
        this.addForm.way = this.missionDetail.way
        if (this.missionDetail.picture !== undefined) {
          this.addForm.picture = this.missionDetail.picture.split(',')
            .map((picture) => picture)
        }

        this.addForm.remark = this.missionDetail.remark
      }
    }
  }
}
</script>
<style scoped lang="scss">
.addfarmsStock {
  width: 80.2083vw;
  height: 47.4375vw * 0.834;
  background: #ffffff;
  border-radius: 20px 20px 20px 20px;
  position: relative;
  padding: 4.5313vw * 0.834 0 0  13.75vw;
}
.back {
  position: absolute;
  top: 5%;
  left: 5%;
}
.demo-ruleForm {
  ::v-deep .el-input__inner {
    width: 36.4583vw !important;
  }
}
.name {
  display: inline-block;
  ::v-deep .el-form-item__content {
    display: flex;
  }
  .addBtn {
    margin-left: .5208vw;
  }
}
::v-deep .farmsStockDialog {
  width: 80.2083vw;
  height: 40.4375vw * 0.834;
  background: #ffffff;
  border-radius: 20px 20px 20px 20px;
}
</style>
<style>
/* .el-dialog__header {
  background: #002a52;
  text-align: center;
} */
.el-dialog__title {
  border-left: 3px solid #19b560;
  padding: 0 0.5208vw;
  font-weight: 500;
}
</style>
